<style scoped>
.dialogM /deep/ .el-dialog__body {
  padding-bottom: 10px;
}
</style>
<!--  -->
<template>
  <div class="dialogM">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @closed="close" :close-on-click-modal="false" width="800px">
      <el-form :inline="true" :model="urlitem" v-loading="loading" :rules="rules" :label-width="formLabelWidth" ref="formValidate">
        <el-row>
          <el-col :span="12">
            <el-form-item label="来源链接" prop="fromUrl">
              <el-input :disabled="disabled" size="medium" v-model="urlitem.fromUrl" autocomplete="off" style="width: 265px;"
                placeholder="">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目标链接" prop="toUrl">
              <el-input size="medium" v-model="urlitem.toUrl" autocomplete="off" style="width: 265px;"
                placeholder="">
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="状态：">
          <el-radio-group v-model="urlitem.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  props: {
    resetData: {
      type: Function
    }
  },
  data () {
    var checkfromUrl = (rule, value, callback) => {
      if (this.urlitem.type === 0 && value.trim() === '') {
        callback(new Error('请输入来源链接'))
      } else {
        return callback()
      }
    }
    return {
      dialogFormVisible: false,
      title: '',
      urlitem: {
        fromUrl: '',
        status: 0,
        toUrl: '',
        type: 0
      },
      formLabelWidth: '100px',
      loading: false,
      disabled: false,
      rules: {
        fromUrl: [
          // { required: true, message: '请输入来源链接', trigger: 'blur' }
          { validator: checkfromUrl, trigger: 'blur' }
        ],
        toUrl: [
          { required: true, message: '请输入目标链接', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
  },
  methods: {
    close () {
      if (this.$refs['formValidate']) {
        this.$refs['formValidate'].clearValidate()
      }
      this.dialogFormVisible = false
      this.loading = false
      this.disabled = false
      this.title = ''
      this.urlitem = {
        fromUrl: '',
        id: 0,
        status: 0,
        toUrl: '',
        type: 0
      }
    },
    show (data) {
      this.dialogFormVisible = true
      if (data) {
        this.title = '编辑链接重定向'
        this.urlitem = JSON.parse(JSON.stringify(data))
        if (this.urlitem.type === 1) {
          this.disabled = true
        }
      } else {
        this.title = '添加链接重定向'
      }
    },
    submit () {
      this.loading = true
      this.$refs['formValidate'].validate((valid) => {
        if (valid) {
          this.$jpost('/host-redirect-url/save', this.urlitem).then(response => {
            if (response && response.length > 0) {
              this.dialogFormVisible = false
              this.resetData()
            }
            this.loading = false
          })
        } else {
          this.loading = false
        }
      })
    }
  }
}
</script>
